package com.yanqu.road.server.manger.activity.eggs;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.eggs.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.EggsActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.eggs.EggsActivityModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

public class EggsActivityMgr extends TempMgr {
    private static EggsActivityConfig eggsActivityConfig;
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload eggs activity start");

        // 获取活动
        ActivityInfo activityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.EggsActivity.getValue());
        if (openActivityInfoList.size() <= 0) {
            eggsActivityConfig = null;
            getLogger().info("no eggs activity in show time");
            return;
        }
        activityInfo = openActivityInfoList.get(0);

        Map<Integer, EggsClassRoomConfig> classRoomConfigMap = EggsActivityBussiness.getEggsClassRoomConfigMap(activityInfo.getActivityId());
        Map<Integer, EggsTestConfig> testConfigMap = EggsActivityBussiness.getEggsTestConfigMap(activityInfo.getActivityId());
        Map<Integer, EggsSongConfig> songConfigMap = EggsActivityBussiness.getEggsSongConfigMap(activityInfo.getActivityId());
        Map<Integer, EggsStoryConfig> storyConfigMap = EggsActivityBussiness.getEggsStoryConfigMap(activityInfo.getActivityId());
        Map<Integer, EggsSceneConfig> sceneConfigMap = EggsActivityBussiness.getEggsSceneConfigMap(activityInfo.getActivityId());

        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        EggsActivityConfig tempConfig = new EggsActivityConfig();
        tempConfig.setActivityInfo(activityInfo);
        tempConfig.initConfig(activityConfigMap, classRoomConfigMap, testConfigMap, songConfigMap,storyConfigMap, sceneConfigMap);
        eggsActivityConfig = tempConfig;

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            EggsActivityModule eggsActivityModule = player.getModule(EggsActivityModule.class);
            eggsActivityModule.syncActivityConfig();
            eggsActivityModule.initUserData();
            eggsActivityModule.syncUserData();
        }
        getLogger().info("reload eggs activity end");
    }

    public static EggsActivityConfig getEggsActivityConfig() {
        return eggsActivityConfig;
    }

    /**
    * 描述：活动是否在展示期
    * 作者：zrq
    * 时间：2022/11/23
    * 参数：

    * 返回值：
    **/
    public static boolean isActivityInShowTime(){
        if(null == eggsActivityConfig){
            return false;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(eggsActivityConfig.getActivityId());
        return ActivityHelper.activityInShowTime(activityInfo);
    }
    public static boolean isActivityInShowTime(int activityId){
        if(!isActivityInShowTime()){
            return false;
        }
        if(null == eggsActivityConfig){
            return false;
        }
        if(eggsActivityConfig.getActivityId() != activityId){
            return false;
        }
        return true;
    }
}
